package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    // 配置跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 允许所有接口跨域
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH")
                .maxAge(3600);
    }

    // 配置静态资源（图片访问）
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射 /city-images/** 到项目内 static/city-images 目录
        registry.addResourceHandler("/city-images/**")
                .addResourceLocations("classpath:/static/city-images/")
                .setCachePeriod(0);
    }
}